package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.Sign;
import com.sky.repository.SignRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SignServiceImpl {
    @Autowired
    private SignRepository signRepository;

    /**
     * 获取签到数据
     */
    public Sign loadSignData() {
        Integer id = BaseContext.getCurrentId();
        return signRepository.findByuId(id);
    }

    /**
     * 保存签到数据
     */
    public void saveSignData(Sign sign) {
        Integer uId = BaseContext.getCurrentId();

        // 确保Sign对象中的uId与当前登录用户一致
        sign.setUId(uId);

        Sign existingSign = signRepository.findByuId(uId);
        if (existingSign == null) {
            // 新用户签到，直接保存
            signRepository.save(sign);
        } else {
            signRepository.deleteSignByuId(uId);
            BeanUtils.copyProperties(sign, existingSign);
            signRepository.save(existingSign); // 使用save方法更新现有实体
        }
    }
}